use spade::Triangulation;

use crate::approx::point::ApproxPoint;

pub fn triangles(
    points_from_half_edges: impl IntoIterator<Item = ApproxPoint<2>>,
    points_from_surface: impl IntoIterator<Item = ApproxPoint<2>>,
) -> Vec<[ApproxPoint<2>; 3]> {
    let mut triangulation = spade::ConstrainedDelaunayTriangulation::<_>::new();

    // We're passing duplicate points to the triangulation here. It doesn't seem
    // to mind though.
    triangulation
        .add_constraint_edges(points_from_half_edges, true)
        .unwrap();

    for point in points_from_surface {
        triangulation.insert(point).unwrap();
    }

    triangulation
        .inner_faces()
        .map(|triangle| triangle.vertices().map(|vertex| *vertex.data()))
        .collect()
}
